package com.iyao_app.mvp.guide;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.iyao_app.R;
import com.iyao_app.adapter.MyViewPagerAdapter;
import com.iyao_app.entry.User;
import com.iyao_app.mvp.home.MainActivity;
import com.iyao_app.mvp.login.LoginActivity;
import com.iyao_app.util.Contants;
import com.iyao_app.util.DbUtil;
import com.iyaowoqu.base_library.util.LogUtil;
import com.iyaowoqu.base_library.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/29.
 */

public class SplashActivity extends AppCompatActivity {
    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.tv_enter)
    TextView mTvEnter;
    @BindView(R.id.rg_indicate)
    RadioGroup mRgIndicate;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);

        final List<Integer> images = new ArrayList<>();
        images.add(R.mipmap.splash1);
        images.add(R.mipmap.splash2);
        images.add(R.mipmap.splash3);
        List<View> views = new ArrayList<View>();

        for (int i = 0; i < images.size(); i++) {
            ImageView imageView = (ImageView) LayoutInflater.from(this)
                    .inflate(R.layout.bannar_image, null);
            imageView.setImageResource(images.get(i));
            views.add(imageView);
            // 添加指示器
            addNavigation(i);//每一页添加一个RadioButton，默认选中第一个
        }

        MyViewPagerAdapter pageAdapter = new MyViewPagerAdapter(views);
        mViewpager.setAdapter(pageAdapter);
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton radioButton = (RadioButton) mRgIndicate.getChildAt(position);
                radioButton.setChecked(true);
                if (position==images.size()-1){
                    mTvEnter.setVisibility(View.VISIBLE);
                }else {
                    mTvEnter.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void addNavigation(int index) {
        RadioButton rb = new RadioButton(this);
        rb.setButtonDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        rb.setBackgroundResource(R.drawable.select_notify_friend_dot);
        RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(16, 16);
        params.setMargins(17, 0, 0, 0);
        params.gravity = Gravity.CENTER;
        mRgIndicate.addView(rb, params);
        if (index == 0) {
            rb.setChecked(true);
        }
        // 点击效果
    }

    @OnClick(R.id.tv_enter)
    public void onViewClicked() {
        SPUtils.getInstance().put(Contants.IS_FIRST, false);

        User user = DbUtil.getUser();
        if (user == null) {
            LogUtil.e("====空的===");
            enterLogin();
        } else {
            LogUtil.e("====userInfo===" + user.id + "===" + user.toString());
            enterMain();
        }
    }

    private void enterMain() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void enterLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
